package com.itheima.organization.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.aom.pojo.Aom;
import com.itheima.aom.pojo.AomRole;
import com.itheima.aom.pojo.QueryPageBean;
import com.itheima.organization.dao.AomRoleMapper;
import com.itheima.organization.service.AomRoleService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

@Service
public class AomRoleServiceImpl implements AomRoleService {

    @Autowired
    private AomRoleMapper aomRoleMapper;

    /**
     * 新增角色
     * @param aomRole
     */
    @Override
    public void addRole(AomRole aomRole) {
        aomRoleMapper.insertSelective(aomRole);
    }

    /**
     * 修改角色
     * @param aomRole
     */
    @Override
    public void updateRole(AomRole aomRole) {
        aomRoleMapper.updateByPrimaryKeySelective(aomRole);
    }

    /**
     * 删除角色
     * @param aomRole
     */
    @Override
    public void delete(AomRole aomRole) {
        aomRoleMapper.deleteByPrimaryKey(aomRole);
    }

    /**
     * 分页查询
     * @param map
     * @param page
     * @param size
     * @return
     */
    @Override
    public Page<AomRole> findPage(Map<String, Object> map, Integer page, Integer size) {
        PageHelper.startPage(page, size);
        Example example = createExample(map);
        List<AomRole> aomRoles = aomRoleMapper.selectByExample(example);
        return (Page<AomRole>) aomRoles;
    }


    private Example createExample(Map<String, Object> searchMap){
        Example example = new Example(Aom.class);
        Example.Criteria criteria = example.createCriteria();
        if (searchMap != null){
            //角色id
            if (searchMap.get("id") != null && !"".equals(searchMap.get("id"))){
                criteria.andEqualTo("id", searchMap.get("id"));
            }
            //角色名
            if (searchMap.get("rolename") != null && !"".equals(searchMap.get("rolename"))){
                criteria.andLike("rolename", "%" + searchMap.get("rolename") + "%");
            }
            //说明
            if (searchMap.get("info") != null && !"".equals(searchMap.get("info"))){
                criteria.andLike("info", "%" + searchMap.get("info") + "%");
            }

        }
        return example;
    }

}
